<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
          + request.getServerName() + ":" + request.getServerPort()
          + path + "/";
%>
<html>
<head>
    <title>对比任务配置</title>
  <link rel="stylesheet" type="text/css" href="<%=basePath%>css/easyui.css" />
  <link rel="stylesheet" type="text/css" href="<%=basePath%>css/icon.css" />
  <script type="text/javascript" src="<%=basePath%>js/jquery.min.js"></script>
  <script type="text/javascript" src="<%=basePath%>js/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=basePath%>js/easyui-lang-zh_CN.js"></script>
  <script type="text/javascript" src="<%=basePath%>js/json2.js"></script>
</head>
<body>
<div id="main" class="easyui-layout" fit="true">
  <div id="center" region="center" style="background:#eee; overflow:hidden;border:0px;">
    <table toolbar="#div_toolbar" id="dataGrid" fitColumns="true" fit="true"  style="width:auto;height:100%"></table>
  </div>
  <div id="div_toolbar">
    <!-- 新增按钮-->
    <a id="new" class="easyui-linkbutton" title="新建" iconCls="icon-add" plain="true">新建</a>
    <!-- 保存按钮-->
    <a id="save" class="easyui-linkbutton" style="display: none;" title="保存" iconCls="icon-save" plain="true">保存</a>
    <!-- 删除按钮-->
    <a id="del" class="easyui-linkbutton" title="删除" iconCls="icon-cancel" plain="true">删除</a>
    <!-- 刷新按钮-->
    <a id="ref" class="easyui-linkbutton" title="刷新" iconCls="icon-reload" plain="true">刷新</a>
    <!-- 手动备份按钮-->
    <%--<input type="text" id="filed" class="easyui-textbox" style="border:1px solid #94BAE7;"/>--%>
    <%--<a id="search" style="color: #313031; border:1px solid #E7F3F7;" class="easyui-linkbutton" title="查询" iconCls="icon-search" plain="true"></a>--%>
  </div>
</div>

<%--编辑框--%>
<div id="new-win" class="easyui-window" title="新建" data-options="iconCls:'icon-save',closed:true,footer:'#footer'" style="width:400px;height:500px;padding:30px">
    <input  type="hidden" id="contrast_id"/>
    <div style="margin-bottom:20px">
      <select class="easyui-combobox" id="contrast" data-options="valueField:'value',textField:'text'" label="备份任务描述:" style="width:100%"></select>
    </div>
  <div style="margin-bottom:20px">
    <select class="easyui-combobox" id="table-column"  data-options="multiple:true,valueField:'value',textField:'text'" label="对比列名:" style="width:100%"></select>
  </div>
    <div style="margin-bottom:20px">
      <input class="easyui-textbox" id="timer" style="width:100%" data-options="label:'监控时间:',required:true,missingMessage:'以分钟为单位，必填项'">
    </div>

  <div id="footer" style="text-align:center;padding:10px 0;">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="save()" style="width:80px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeWindow()" style="width:80px">关闭</a>
  </div>
</div>
</body>
<script type="text/javascript">
  var offset = 1;
  var limit = 25;
  var contrastID;
  $(function(){
    loadPages({offset:offset,limit:limit});
    dataGrid = $('#dataGrid').datagrid({//表格数据
      singleSelect:false,
      pagination:true,
      nowrap:false,
      columns:[[
        {field:'ck',checkbox:true},
        {field:'id',title:'标示',hidden:true,align:"center",sortable:true},
        {field:'sds_name',title:'数据源别名',align:"center",width:'12%',sortable:true,editor:'text'},
        {field:'src_tab',title:'数据源表名',align:'center',width:'12%',sortable:true},
        {field:'tds_name',title:'备份数据名',align:'center',width:'15%',sortable:true},
        {field:'tgt_tab',title:'备份表名',align:'center',width:'20%',resizable:true,editor:'text'},
        {field:'compare_fileds',title:'对比列名',align:'center',width:'14%',sortable:true,editor:'text'},
        {field:'compare_time_quantum',title:'监控时间',align:'center',width:'12%',sortable:true,editor:'text'},
        {field:'createtime',title:'创建时间',align:'center',width:'10%',resizable:true}
      ]]
    });

    $("#contrast").combobox({
      onSelect: function (record) {
        contrastID = record.value;
        console.log(contrastID)
        $.ajax({
          url:'<%=basePath%>taskCompare/getColumn',
          type:'POST',
          timeout:20000,
          data:{
            id:contrastID
          },
          success:function (res){
            console.log(res)
            if(res.code ==0) {
              $("#table-column").combobox("loadData", res.data.data);
            }
          },
          error: function (error) {
            $.messager.alert("系统繁忙，请稍后再试！");
          }
        });
      }
    });

    $("#new").bind("click",function(){
      $('#new-win').window('open');
        $.ajax({
          url:'<%=basePath%>taskCompare/getContrast',
          type:'POST',
          timeout:20000,
          success:function (res){
            if(res.code ==0) {
              $("#contrast").combobox("loadData", res.data.data);
            }
          },
          error: function (error) {
            $.messager.alert("系统繁忙，请稍后再试！");
          }
        });
    });

    $("#del").bind("click",function(){//删除按钮
      var rows = dataGrid.datagrid("getSelections");
      if(rows != undefined){
        var arr ="";
        for(var i = 0;i < rows.length; i++){
          if(rows[i] != undefined)
            arr +=rows[i].id+",";
        }
        console.log(arr)
        $.ajax({
          url:"<%=basePath%>taskCompare/delete?id=" + 1,
          type:"post",
          timeout:20000,
          data:{
            arr:arr
          },
          success:function(res){
            if(res.code == 0) {
              loadPages({offset:1,limit:limit});
            }
            $.messager.show({
              title:'提示',
              width:250,
              height:140,
              msg:res.message
            });
          },
          error: function (error) {
            $.messager.show({
              title:'提示',
              width:250,
              height:140,
              msg:"系统繁忙，请稍后再试！"
            });
          }
        });
      } else {
        $.messager.show({
          title:'提示',
          width:250,
          height:140,
          msg:"<div class='messager-icon messager-error'></div><div>没有选中行！</div>"
        });
      }

    });

  });

  /**加载数据分页方法**/
  function loadPages(param){
    $.ajax({
      url:'<%=basePath%>taskCompare/findPages',
      type:'POST',
      data:param,
      timeout:20000,
      success:function(res){
        console.log(res)
        if(res.code == 0){
          $("#dataGrid").datagrid("loadData",res.data.data);
          total =res.data.total;
          //grid的分页信息
          $('#dataGrid').datagrid('getPager').pagination({//分页对象
            pageSize: 25,
            total: res.data.total,
            pageNumber: res.data.offset,
            pageList:[25,50,75,100],
            onSelectPage : function(pageNumber, pageSize) {
              var param ={offset:pageNumber,limit:pageSize}
              loadPages(param);
            }
          });
        } else {
          $.messager.show({
            title:'提示',
            width:250,
            height:140,
            msg:"没有任何数据，请先添加数据！"
          });
        }
      },
      error: function (error) {
        $.messager.show({
          title:'提示',
          width:250,
          height:140,
          msg:"<div class='messager-icon messager-error'></div><div>系统繁忙，请稍后再试！</div>"
        });
      }
    });
  }

  function save(){
    var column = $("#table-column").combobox("getText");
    var timer = $("#timer").textbox("getValue");
    console.log(column + '   ' + timer)
    $.ajax({
      url:'<%=basePath%>taskCompare/save',
      type:'POST',
      data:{
        plugContrastId:contrastID,
        compareFileds:column,
        compareTimeQuantum:timer
      },
      timeout:20000,
      success:function(res){
        console.log(res)
        if(res.code == 0){
          loadPages({offset:1,limit:limit});
        }
        $.messager.show({
          title:'提示',
          width:250,
          height:140,
          msg:res.message
        });
      },
      error: function (error) {
        $.messager.show({
          title:'提示',
          width:250,
          height:140,
          msg:"<div class='messager-icon messager-error'></div><div>系统繁忙，请稍后再试！</div>"
        });
      }
    });
  }

  function closeWindow(){
    $('#new-win').window('close');
  }
</script>
</html>
